package com.icetech.eom.service.impl.tx;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.icetech.eom.dao.sys.CustomDao;
import com.icetech.eom.dao.tx.DeviceDao;
import com.icetech.eom.domain.tx.Device;
import com.icetech.eom.domain.tx.dto.DeviceDto;
import com.icetech.eom.domain.tx.dto.DeviceExcelDto;
import com.icetech.eom.service.tx.IDeviceService;
import com.xuxueli.poi.excel.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangzw
 * @since 2018-11-12
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceDao, Device> implements IDeviceService {

    @Autowired
    private DeviceDao deviceDao;

    @Override
    public PageInfo<DeviceDto> selectByPage(String searchKey, Integer page, Integer pageSize,Long orgId) {
        PageHelper.startPage(page,pageSize);
        List<DeviceDto> devices = deviceDao.selectBySearchKey(searchKey,orgId);
        return new PageInfo(devices);
    }

    @Override
    public Workbook exportExcel(String searchKey,Long orgId) {
        List<DeviceDto> deviceDtos = deviceDao.selectBySearchKey(searchKey,orgId);
        List<DeviceExcelDto> deviceExcelDtos = Lists.newArrayList();
        deviceDtos.forEach(deviceDto -> {
            DeviceExcelDto deviceExcelDto = new DeviceExcelDto();
            BeanUtils.copyProperties(deviceDto, deviceExcelDto);
            if (deviceDto.getIsEnabled()){
                deviceExcelDto.setIsEnabledStr("可用");
            }else {
                deviceExcelDto.setIsEnabledStr("不可用");
            }
            deviceExcelDtos.add(deviceExcelDto);
        });
        return ExcelExportUtil.exportWorkbook(deviceExcelDtos);
    }

    /**
     * 获取指定用户下绑定的的设备信息
     * @param searchKey
     * @param curpage
     * @param userid
     * @return
     */
    @Override
    public PageInfo<Device> selectByUserIdPage(String searchKey, Integer curpage, Integer pageSize, Long userid) {
        // curpage = 0 时为不分页
        if (curpage!= 0){
            PageHelper.startPage(curpage,pageSize);
        }else {
            PageHelper.startPage(1,Integer.MAX_VALUE);
        }
        List<Device>devices =deviceDao.selectByUserIdAndSearchKey(searchKey,userid);
        return new PageInfo<>(devices);
    }

    @Override
    public Device findByUid(String guid) {
        return deviceDao.selectByGuid(guid);
    }

    @Override
    public PageInfo<DeviceDto> selectListUnBindDevice(Integer page, Integer pageSize, Long userId, String uid, String fromUid, String toUid, Long orgId) {
        PageHelper.startPage(page,pageSize);
        //获取所有未绑定当前用户的设备
        List<DeviceDto> unBindDevices = deviceDao.selectListUnBindDevice(userId,uid,fromUid,toUid,orgId);
        return new PageInfo(unBindDevices);
    }
}
